from datetime import datetime

from flask import Flask
from flask_login import UserMixin

from Database.init import get_flask_db

app = Flask(__name__)  # 创建一个wsgi应用
db = get_flask_db(app)


class AdminImportEmail(db.Model):
    __tablename__ = "weline_admin_import_email"  # 数据库中对应的表的名称
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(128), unique=True)
    importer_user_id = db.Column(db.Integer)
    is_used = db.Column(db.Integer, default=0)
    used_user_id = db.Column(db.Integer, default=0)
    is_send = db.Column(db.Integer, default=0)
    create_time = db.Column(db.DateTime, default=datetime.now())

    def get_id(self):
        return self.id


class AdminUser(UserMixin, db.Model):
    __tablename__ = "weline_admin_user"  # 数据库中对应的表的名称
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(16), unique=True)
    email = db.Column(db.String(128), default=None)
    password = db.Column(db.String(256), default=None)
    active = db.Column(db.Integer, default=1)
    create_time = db.Column(db.DateTime, default=datetime.now())

    def get_id(self):
        return self.id

    def is_authenticated(self):
        return True

    def is_active(self):
        return self.active

    def is_anonymous(self):
        return False


class UploadLog(db.Model):
    db = db
    __tablename__ = "weline_upload_log"  # 数据库中对应的表的名称
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, default=0)
    admin_user_id = db.Column(db.Integer, default=0)
    file_path = db.Column(db.String(255), default='')
    create_time = db.Column(db.DateTime, default=datetime.now())

    def get_id(self):
        return self.id

    def get_user_id(self):
        return self.user_id

    def get_admin_user_id(self):
        return self.admin_user_id


class User(UserMixin, db.Model):
    __tablename__ = "weline_user"  # 数据库中对应的表的名称
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(16), unique=True)
    email = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(256), default=None)
    active = db.Column(db.Integer, default=1)
    create_time = db.Column(db.DateTime, default=datetime.now())

    def get_id(self):
        return self.id

    def is_authenticated(self):
        return True

    def is_active(self):
        return self.active

    def is_anonymous(self):
        return False


class UserSmtp(db.Model):
    __tablename__ = "weline_user_smtp"  # 数据库中对应的表的名称
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, unique=True)
    host = db.Column(db.String(128), default=None)
    port = db.Column(db.String(128), default=None)
    email = db.Column(db.String(128), default=None)
    password = db.Column(db.String(256), default=None)
    active = db.Column(db.Integer, default=1)
    create_time = db.Column(db.DateTime, default=datetime.now())


class UserEmailTemplate(db.Model):
    __tablename__ = "weline_user_email_template"  # 数据库中对应的表的名称
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, unique=True)
    subject = db.Column(db.String(128), default=None)
    sender_nickname = db.Column(db.String(128), default=None)
    content = db.Column(db.String(1000), default=None)
    active = db.Column(db.Integer, default=1)
    create_time = db.Column(db.DateTime, default=datetime.now())


if __name__ == '__main__':
    db.create_all()
